import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { execSync } from 'child_process'
import minimist from 'minimist'
const args = minimist(process.argv.slice(2))
const target = args._[0] || 'vue'
const formats = args.formats || 'global'
run()
async function run() {
  const __dirname = path.dirname(fileURLToPath(import.meta.url))
  const pkgDir = path.resolve(__dirname, `../packages/${target}`)
  const pkg = JSON.parse(fs.readFileSync(`${pkgDir}/package.json`, 'utf-8'))
  
  // 清空 dist 目录
  fs.rmSync(`${pkgDir}/dist`, { recursive: true, force: true })
  fs.mkdirSync(`${pkgDir}/dist`, { recursive: true })
  
  // 执行构建，使用根目录的 rollup.config.js
  execSync('rollup -c ../../rollup.config.js --environment TARGET:' + target, { stdio: 'inherit' })
  
  console.log(`Successfully built ${target}!`)
}